package com.jifeng.sterilizer.controller;

import com.jifeng.sterilizer.service.DeviceService;
import com.jifeng.sterilizer.utils.R;
import com.jifeng.sterilizer.vo.DeviceEntityVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


/**
 * 消毒机表
 *
 * @author jifeng
 * @email 1593140625@qq.com
 * @date 2020-07-26 15:15:14
 */
@RestController
@RequestMapping("sterilizer/device")
public class DeviceController {

    @Autowired
    private DeviceService deviceService;

    /**
     * 设置消毒机出厂时间 PC
     * @param params 设备编号
     * @return 修改出厂时间结果
     */
    @PostMapping("/pc/management/changeTime")
    @CacheEvict(value = {"device","device:farm"},key = "#params.get('number')")
    public R updateDeviceProduceTime(@RequestBody Map<String, Object> params) {
        return deviceService.updateDeviceProduceTime(params);
    }

    /**
     * 智能计算
     * @param params length、width、height、number
     * @return 计算结果
     */
    @PostMapping("/calculateWorkTime")
    public R calculateDeviceWorkTime(@RequestBody Map<String, Object> params) {
        return deviceService.calculateDeviceWorkTime(params);
    }

    /**
     * 智能计算 PC
     * @param params length、width、height、number
     * @return 计算结果
     */
    @PostMapping("/pc/calculateWorkTime")
    public R calculateWorkTimePc(@RequestBody Map<String, Object> params) {
        return deviceService.calculateDeviceWorkTime(params);
    }

    /**
     * 监控 PC
     * @param params 消毒机编号
     * @return 消毒机和农场信息
     */
    @GetMapping("/pc/get")
    @Cacheable(value = "device:farm",key = "#params.get('device_id')",unless = "#result.get('code')!=200")
    public R monitor(@RequestParam Map<String, Object> params) {
        return deviceService.monitor(params);
    }

    /**
     * 上限|下限温度|运行定时|加热状态|工作状态修改|清洗保养|后台/本机 PC
     * @param params 参数
     * @return 修改结果
     */
    @CacheEvict(value = {"device","device:farm"},key = "#params.get('number')")
    @PostMapping("/pc/callDevUpdate")
    public R callDevUpdate(@RequestBody Map<String, Object> params) {
        return deviceService.callDevUpdate(params);
    }

    /**
     * 定时设置 PC
     * @param params 参数
     * @return 设置结果
     */
    @CacheEvict(value = {"device","device:farm"},key = "#params.get('number')")
    @PostMapping("/pc/callDevUpdateTimeRun")
    public R callDevUpdateTimeRun(@RequestBody Map<String, Object> params) {
        return deviceService.callDevUpdateTimeRun(params);
    }

    /**
     * 设置系统时间 PC
     * @param params 参数
     * @return 设置结果
     */
    @PostMapping("/pc/setSystemTime")
    @CacheEvict(value = {"device","device:farm"},key = "#params.get('number')")
    public R setSystemTime(@RequestBody Map<String, Object> params) {
        R rs = deviceService.setSystemTime(params);
        return rs;
    }

    /**
     * 获取消毒机信息 微信小程序
     * @param params 消毒机编号和用户ID
     * @return 消毒机信息
     */
    @Cacheable(value = "device", key = "#params.get('device_id')", unless = "#result.get('code')!=200")
    @GetMapping("/wx/info")
    public R getDeviceInfoWx(@RequestParam Map<String, String> params) {
        return deviceService.getDeviceInfoWx(params);
    }

    /**
     * 获取消毒机信息
     * @param params 消毒机编号
     * @return 消毒机信息
     */
    @GetMapping("/info")
    public R getDeviceInfo(@RequestParam Map<String, Object> params) {
        return deviceService.getDevicesInfo(params);
    }

    /**
     * 新增消毒机 PC
     * @param params 消毒机信息
     * @return 新增结果
     */
    @PostMapping("/pc/management/add")
    public R addDevice(@RequestBody Map<String, Object> params) {
        return deviceService.addDevice(params);
    }

    /**
     * 修改消毒机 PC
     * @param params 消毒机信息
     * @return 修改结果
     */
    @PostMapping("/pc/management/update")
    @CacheEvict(value = {"device","device:farm"},key = "#params.get('number')")
    public R updateDevice(@RequestBody Map<String, Object> params) {
        return deviceService.updateDevice(params);
    }

    /**
     * 删除消毒机 PC
     * @param params 消毒机编号
     * @return 删除结果
     */
    @PostMapping("/pc/management/del")
    @CacheEvict(value = {"device","device:farm"},key = "#params.get('number')")
    public R deleteDevice(@RequestBody Map<String, Object> params) {
        return deviceService.deleteDevice(params);
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @CacheEvict(value = {"device","device:farm"},key = "#device.number")
    public R update(@RequestBody DeviceEntityVo device) {
        R rs = deviceService.updateByNumber(device);
        return rs;
    }
}
